<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">用户列表</a></el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索、添加、自定义列 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input placeholder="请输入姓名" v-model="query.name">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="showAddDialog">添加用户</el-button>
        </el-col>
        <el-col :span="3">
          <el-button type="success" @click="showSetColumn">自定义列</el-button>
        </el-col>
      </el-row>
      <!-- 内容列表 -->
      <el-table :data="list" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '账户').show"
          label="账号"
          prop="account"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '姓名').show"
          label="姓名"
          prop="name"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '角色').show"
          label="角色"
          prop="roleType"
          width="80"
        >
          <template slot-scope="scope">
            {{
              scope.row.roleType == 0
                ? "学生"
                : scope.row.roleType == 1
                ? "教师"
                : scope.row.roleType == 2
                ? "数据中心管理员"
                : scope.row.roleType == 4
                ? "高级教师"
                : "系统管理员"
            }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '分院').show"
          label="分院"
          prop="brcachMame"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '班级').show"
          label="班级"
          prop="className"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '教师编号').show"
          label="教师编号"
          prop="teacherCode"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '性别').show"
          label="性别"
          prop="sex"
          width="50"
        >
          <template slot-scope="scope">
            {{ scope.row.sex == 0 ? "男" : scope.row.sex == 1 ? "女" : "未知" }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '职称').show"
          label="职称"
          prop="title"
          width="80"
        ></el-table-column>
        <!-- <el-table-column label="头像短链接" prop="portrait" width="100"></el-table-column> -->
        <el-table-column
          v-if="showColumns.find((i) => i.label == '手机号').show"
          label="手机号"
          prop="mobile"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '电子邮箱').show"
          label="电子邮箱"
          prop="email"
          width="120"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '昵称').show"
          label="昵称"
          prop="nickname"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '出生日期').show"
          label="出生日期"
          prop="birthday"
          width="120"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '办公电话').show"
          label="办公电话"
          prop="officePhone"
          width="120"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '创建时间').show"
          label="创建时间"
          prop="createTime"
          width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.createTime.substring(0, 16) }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '修改时间').show"
          label="修改时间"
          prop="modifyTime"
          width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.modifyTime.substring(0, 16) }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '备注').show"
          label="备注"
          prop="remarks"
        ></el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-tooltip effect="dark" content="查看" placement="top">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="showDetailDialog(scope.row)"
              ></el-button>
            </el-tooltip>
            <!-- 修改 -->
            <el-tooltip effect="dark" content="修改" placement="top">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope.row)"
              ></el-button>
            </el-tooltip>
            <!-- 重置密码 -->
            <el-tooltip
              v-if="scope.row.account != 'admin'"
              effect="dark"
              content="重置密码"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="resetPwd(scope.row.id)"
              ></el-button>
            </el-tooltip>
            <!-- 删除 -->
            <el-tooltip
              v-if="scope.row.account != 'admin'"
              effect="dark"
              content="删除"
              placement="top"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="del(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 自定义列 -->
    <CustomColumn ref="columns" :showColumns="showColumns"></CustomColumn>

    <!-- 详情 -->
    <el-dialog
      title="用户详情"
      :visible.sync="showDetailVisible"
      width="70%"
      @close="hideDetailDialog"
      center
    >
      <el-form :model="detail" label-width="80px">
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="账号:">
              <el-input v-model="detail.account" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名:">
              <el-input v-model="detail.name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="角色:">
              <el-select disabled style="width: 100%" v-model="detail.roleType">
                <el-option label="学生" :value="0"></el-option>
                <el-option label="教师" :value="1"></el-option>
                <el-option label="数据中心管理员" :value="2"></el-option>
                <el-option label="高级教师" :value="4"></el-option>
                <el-option label="系统管理员" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="分院:">
              <el-input v-model="detail.brcachMame" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班级:">
              <el-input v-model="detail.className" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="教师编号:">
              <el-input v-model="detail.teacherCode" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="性别:">
              <el-select disabled style="width: 100%" v-model="detail.sex">
                <el-option label="男" :value="0"></el-option>
                <el-option label="女" :value="1"></el-option>
                <el-option label="未知" :value="null"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职称:">
              <el-input v-model="detail.title" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号:">
              <el-input v-model="detail.mobile" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="电子邮箱:">
              <el-input v-model="detail.email" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="昵称:">
              <el-input v-model="detail.nickname" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期:">
              <el-input v-model="detail.birthday" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="办公电话:">
          <el-input v-model="detail.officePhone" disabled></el-input>
        </el-form-item>
        <el-form-item label="创建时间:">
          <el-input v-model="detail.createTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="修改时间:">
          <el-input v-model="detail.modifyTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input
            type="textarea"
            v-model="detail.remarks"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="hideDetailDialog">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增 -->
    <el-dialog
      title="新增用户"
      :visible.sync="showAddVisible"
      width="70%"
      @close="hideAddDialog"
      center
    >
      <el-form :model="addForm" ref="addFormRef" label-width="80px">
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item
              label="账号:"
              prop="account"
              :rules="[
                { required: true, message: '账号不能为空', trigger: 'blur' },
                {
                  min: 4,
                  max: 12,
                  message: '长度须在4～12个字符之间',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input v-model="addForm.account"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="密码:"
              prop="password"
              :rules="[
                { required: true, message: '密码不能为空', trigger: 'blur' },
                {
                  min: 6,
                  max: 20,
                  message: '长度须在4～20个字符之间',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input type="password" v-model="addForm.password"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="角色:"
              prop="roleType"
              :rules="[
                { required: true, message: '角色不能为空', trigger: 'blur' },
              ]"
            >
              <el-select style="width: 100%" v-model="addForm.roleType">
                <el-option label="学生" :value="0"></el-option>
                <el-option label="教师" :value="1"></el-option>
                <el-option label="数据中心管理员" :value="2"></el-option>
                <el-option label="高级教师" :value="4"></el-option>
                <el-option label="系统管理员" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="姓名:" prop="name">
              <el-input v-model="addForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分院:">
              <el-input v-model="addForm.brcachMame"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班级:">
              <el-input v-model="addForm.className"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="教师编号:">
              <el-input v-model="addForm.teacherCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职称:">
              <el-input v-model="addForm.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别:">
              <el-select style="width: 100%" v-model="addForm.sex">
                <el-option label="男" :value="0"></el-option>
                <el-option label="女" :value="1"></el-option>
                <el-option label="未知" :value="null"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="电子邮箱:">
              <el-input v-model="addForm.email"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号:">
              <el-input v-model="addForm.mobile"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="办公电话:">
              <el-input v-model="addForm.officePhone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="昵称:">
              <el-input v-model="addForm.nickname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期:">
              <el-date-picker
                v-model="addForm.birthday"
                clearable
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注:">
          <el-input type="textarea" v-model="addForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="hideAddDialog">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改用户"
      :visible.sync="showEditVisible"
      width="70%"
      @close="hideEditDialog"
      center
    >
      <el-form :model="editForm" ref="editFormRef" label-width="80px">
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="账号:" prop="account">
              <el-input v-model="editForm.account" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码:" prop="password">
              <el-input
                type="password"
                v-model="editForm.password"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="角色:"
              prop="roleType"
              :rules="[
                { required: true, message: '角色不能为空', trigger: 'blur' },
              ]"
            >
              <el-select style="width: 100%" v-model="editForm.roleType">
                <el-option label="学生" :value="0"></el-option>
                <el-option label="教师" :value="1"></el-option>
                <el-option label="数据中心管理员" :value="2"></el-option>
                <el-option label="高级教师" :value="4"></el-option>
                <el-option label="系统管理员" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="姓名:">
              <el-input v-model="editForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分院:">
              <el-input v-model="editForm.brcachMame"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班级:">
              <el-input v-model="editForm.className"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="教师编号:">
              <el-input v-model="editForm.teacherCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职称:">
              <el-input v-model="editForm.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别:">
              <el-select style="width: 100%" v-model="editForm.sex">
                <el-option label="男" :value="0"></el-option>
                <el-option label="女" :value="1"></el-option>
                <el-option label="未知" :value="null"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="电子邮箱:">
              <el-input v-model="editForm.email"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号:">
              <el-input v-model="editForm.mobile"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="办公电话:">
              <el-input v-model="editForm.officePhone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="昵称:">
              <el-input v-model="editForm.nickname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期:">
              <el-date-picker
                v-model="editForm.birthday"
                clearable
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注:">
          <el-input type="textarea" v-model="editForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="hideEditDialog">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import md5 from "js-md5";
import CustomColumn from "./components/CustomColumn.vue";
export default {
  components: { CustomColumn },
  data() {
    return {
      // 查询条件
      query: {
        name: "",
        pageNum: 1,
        pageSize: 10,
      },
      // 内容列表
      list: [],
      // 内容总条数
      total: 0,
      // 初始化显示字段
      showColumns: [
        { label: "账户", show: true },
        { label: "角色", show: true },
        { label: "姓名", show: true },
        { label: "分院", show: false },
        { label: "班级", show: false },
        { label: "教师编号", show: true },
        { label: "性别", show: true },
        { label: "职称", show: false },
        { label: "手机号", show: false },
        { label: "电子邮箱", show: false },
        { label: "昵称", show: false },
        { label: "出生日期", show: false },
        { label: "办公电话", show: false },
        { label: "创建时间", show: true },
        { label: "修改时间", show: false },
        { label: "备注", show: true },
      ],
      // 是否显示详情
      showDetailVisible: false,
      // 数据详情
      detail: {},
      // 是否显示新增
      showAddVisible: false,
      // 新增数据
      addForm: {},
      // 是否显示修改
      showEditVisible: false,
      // 修改数据
      editForm: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 显示自定义列
    showSetColumn() {
      this.$refs.columns.dialogVisible = true;
    },
    // 获取内容列表
    getList() {
      this.$http.post("/v1/api/users/page", this.query).then((res) => {
        if (res.data.code == 0) {
          this.list = res.data.data.list;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(pageSize) {
      this.query.pageSize = pageSize;
      this.getList();
    },
    // 监听 page 改变的事件
    handleCurrentChange(page) {
      this.query.pageNum = page;
      this.getList();
    },
    // 显示详情
    showDetailDialog(row) {
      this.detail = row;
      this.showDetailVisible = true;
    },
    // 隐藏详情
    hideDetailDialog() {
      this.detail = {};
      this.showDetailVisible = false;
    },
    // 显示新增
    showAddDialog() {
      this.addForm = {
        // 备注
        remarks: "",
        // 账号(用户名)
        account: "",
        // 密码(需md5加密)
        password: "",
        // 所在分院，如：计算分院
        brcachMame: "",
        // 教师号
        teacherCode: "",
        // 职称
        title: "",
        // 所在班级，如：计算1803
        className: "",
        // 姓名
        name: "",
        // 头像短链接
        portrait: "",
        // 性别,0:男,1:女
        sex: 0,
        // 昵称
        nickname: "",
        // 出生日期
        birthday: "",
        // 办公电话
        officePhone: "",
        // 手机号
        mobile: "",
        // 电子邮箱
        email: "",
        // 角色类型（枚举），0:普通用户，1:教师用户，2:数据中心管理员，3:系统管理员
        roleType: 0,
      };
      this.showAddVisible = true;
    },
    // 隐藏新增
    hideAddDialog() {
      this.showAddVisible = false;
    },
    // 新增
    add() {
      this.$refs.addFormRef.validate((valid) => {
        if (valid) {
          this.addForm.password = md5(this.addForm.password);
          this.$http.post("/v1/api/users", this.addForm).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.$message.success("新增成功");
              this.showAddVisible = false;
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("请填写必填项");
          return false;
        }
      });
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("/v1/api/users/" + id).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.$message.success("删除成功");
            }
          });
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
    // 显示修改
    showEditDialog(row) {
      this.editForm = row;
      this.showEditVisible = true;
    },
    // 隐藏修改
    hideEditDialog() {
      this.showEditVisible = false;
    },
    // 修改
    edit() {
      this.editForm.password = md5(this.editForm.password);
      this.$refs.editFormRef.validate((valid) => {
        if (valid) {
          this.$http.put("/v1/api/users", this.editForm).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.$message.success("修改成功");
              this.showEditVisible = false;
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("请填写必填项");
          return false;
        }
      });
    },
    // 重置密码
    resetPwd(id) {
      this.$confirm("此操作将重置该用户的密码为123456, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .put("/v1/api/users", { id: id, password: md5("123456") })
            .then((res) => {
              if (res.data.code == 0) {
                this.getList();
                this.$message.success("重置密码成功");
              }
            });
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}
</style>
